package com.huibao.hulibao.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.huibao.hulibao.R;
import com.huibao.hulibao.constant.GlobalConstants;
import com.huibao.hulibao.entity.HuLiEntity;

import java.util.ArrayList;

/**
 * Created by lizhaoyong on 2018/5/8.
 */
public class HuliGvAdapter extends BaseAdapter {

    public ArrayList<HuLiEntity.HuLiData> al_datas;
    public Context context;
    public int type;


    public HuliGvAdapter(Context context, ArrayList<HuLiEntity.HuLiData> al_datas, int type) {
        this.context = context;
        this.al_datas = al_datas;
        this.type = type;
    }

    @Override
    public int getCount() {
        return al_datas.size();
    }

    @Override
    public Object getItem(int i) {
        return al_datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View v;
        HuLiEntity.HuLiData hd = al_datas.get(i);
        ImageView iv;
        // 妈妈护理1宝宝护理2
        if (type == 1) {
            v = View.inflate(context, R.layout.item_gv_mom_nurse, null);
            TextView tv = (TextView) v.findViewById(R.id.tv_gv_mom_content);
            iv = (ImageView) v.findViewById(R.id.iv_gv_mom);
            tv.setText(hd.n.trim());
            // LogUtils.d("img = " + GlobalConstants.HOST + hd.m);
            Glide.with(context).load(GlobalConstants.HOST + hd.m).listener(new RequestListener<String, GlideDrawable>() {
                // 加载图片失败
                @Override
                public boolean onException(Exception e, String s, Target<GlideDrawable> target, boolean b) {
                    return false;
                }

                // 加载图片成功
                @Override
                public boolean onResourceReady(GlideDrawable glideDrawable, String s, Target<GlideDrawable> target, boolean b, boolean b1) {
                    return false;
                }
            }).error(R.mipmap.huli_gv_defult_mom).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(iv);

        } else {
            v = View.inflate(context, R.layout.item_gv_baby_nurse, null);
            TextView tv = (TextView) v.findViewById(R.id.tv_gv_baby_content);
            iv = (ImageView) v.findViewById(R.id.iv_gv_baby);
            tv.setText(hd.n.trim());
            Glide.with(context).load(GlobalConstants.HOST + hd.m).listener(new RequestListener<String, GlideDrawable>() {
                // 加载图片失败
                @Override
                public boolean onException(Exception e, String s, Target<GlideDrawable> target, boolean b) {
                    return false;
                }

                // 加载图片成功
                @Override
                public boolean onResourceReady(GlideDrawable glideDrawable, String s, Target<GlideDrawable> target, boolean b, boolean b1) {
                    return false;
                }
            }).error(R.mipmap.huli_gv_default_baby).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).into(iv);
        }

        return v;
    }
}
